<template>
  <div 
    class="metric-card" 
    :class="{ clickable: $attrs.onClick }"
    @click="handleClick"
  >
    <div class="metric-content">
      <div class="metric-icon" :style="{ backgroundColor: color + '20' }">
        <el-icon :color="color" :size="24">
          <component :is="icon" />
        </el-icon>
      </div>
      
      <div class="metric-info">
        <div class="metric-value">
          {{ formattedValue }}{{ suffix }}
        </div>
        <div class="metric-title">
          {{ title }}
        </div>
      </div>
      
      <div class="metric-trend" :class="trendClass">
        <el-icon v-if="trend !== 0">
          <CaretTop v-if="trend > 0" />
          <CaretBottom v-else />
        </el-icon>
        <span v-if="trend !== 0">{{ Math.abs(trend) }}%</span>
      </div>
    </div>
    
    <!-- 底部插槽 -->
    <div class="metric-footer" v-if="$slots.footer">
      <slot name="footer"></slot>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { CaretTop, CaretBottom } from '@element-plus/icons-vue'

interface Props {
  title: string
  value: number
  icon: any
  color: string
  trend?: number
  suffix?: string
}

const props = withDefaults(defineProps<Props>(), {
  trend: 0,
  suffix: ''
})

const emit = defineEmits(['click'])

const formattedValue = computed(() => {
  if (props.value >= 10000) {
    return (props.value / 10000).toFixed(1) + '万'
  }
  return props.value.toLocaleString()
})

const trendClass = computed(() => {
  if (props.trend > 0) return 'trend-up'
  if (props.trend < 0) return 'trend-down'
  return 'trend-neutral'
})

const handleClick = () => {
  if (props.$attrs.onClick) {
    emit('click')
  }
}
</script>

<style scoped lang="scss">
.metric-card {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;
  border: 1px solid #f0f0f0;
  
  &.clickable {
    cursor: pointer;
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
      border-color: #e8f4ff;
    }
  }
  
  .metric-content {
    display: flex;
    align-items: center;
    gap: 16px;
    margin-bottom: 12px;
    
    .metric-icon {
      width: 48px;
      height: 48px;
      border-radius: 12px;
      display: flex;
      align-items: center;
      justify-content: center;
      flex-shrink: 0;
    }
    
    .metric-info {
      flex: 1;
      
      .metric-value {
        font-size: 28px;
        font-weight: 700;
        color: #1f2d3d;
        line-height: 1.2;
      }
      
      .metric-title {
        font-size: 14px;
        color: #606266;
        margin-top: 4px;
      }
    }
    
    .metric-trend {
      display: flex;
      align-items: center;
      gap: 4px;
      font-size: 12px;
      font-weight: 500;
      padding: 4px 8px;
      border-radius: 12px;
      
      &.trend-up {
        color: #52c41a;
        background: rgba(82, 196, 26, 0.1);
      }
      
      &.trend-down {
        color: #f5222d;
        background: rgba(245, 34, 45, 0.1);
      }
      
      &.trend-neutral {
        color: #909399;
        background: rgba(144, 147, 153, 0.1);
      }
    }
  }
  
  .metric-footer {
    border-top: 1px solid #f0f0f0;
    padding-top: 12px;
  }
}
</style>